import pygame
from sys import exit

def main():
    pygame.init()
    
    screen = pygame.display.set_mode((400, 300))
    screen.fill((255, 255, 255))
    
    image = pygame.image.load('E:/test.jpg').convert_alpha()
    image = pygame.transform.scale(image, (80, 80))
    screen.blit(image, (0, 0))
    
    pygame.display.flip()
    x = 0
    y = 0
    xspeed = 10
    yspeed = 10
    
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()
                
            # 以下6行是图片跟着鼠标一起移动
            #if event.type == pygame.MOUSEMOTION:
                #x,y = event.pos
                #screen.fill((255, 255, 255))
                #screen.blit(image, (x - 40, y - 40))
                #pygame.display.flip()
            
            # 延迟50毫秒
            pygame.time.delay(50)
            
            # 自动移动
            x += xspeed
            y += yspeed
            screen.fill((255, 255, 255))
            screen.blit(image,(x, y))
            pygame.display.flip()
            
            if x<0 or x+80>400:
                xspeed = -xspeed
            
            if y<0 or y+80>30:
                yspeed = -yspeed

if  __name__ == '__main__':
    main()
                    